export function debounce(fn, delay) {
	let timer = null;
	return function() {
		const _this = this;
		const args = arguments;
		if (timer) {
			clearTimeout(timer);
			console.log('清楚了定时器', timer);
		}
		timer = setTimeout(() => {
			fn.apply(_this, args);
		}, delay);
		console.log('创建了定时器', timer);
	};
}

// 节流函数
export function throttle(func, delay) {
	let lastCall = 0;
	let timeoutId;

	return function(...args) {
		const now = Date.now();

		if (now - lastCall >= delay) {
			// 立即执行
			func.apply(this, args);
			lastCall = now;
		} else {
			// 清除之前的延迟调用
			clearTimeout(timeoutId);

			// 延迟执行，确保在 delay 后可以执行
			timeoutId = setTimeout(() => {
				func.apply(this, args);
				lastCall = Date.now();
			}, delay - (now - lastCall));
		}
	};
}